package reduce.fz.com.reduce.activity;

import android.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.bean.StoryReply;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.fragment.StoryReplyFragment;
import reduce.fz.com.reduce.service.StoryService;
import reduce.fz.com.reduce.util.DateUtil;
import reduce.fz.com.reduce.util.InputMethodUtil;
import reduce.fz.com.reduce.util.ServiceBuilder;

/**
 * 故事回复评界面
 */
public class StoryReplyActivity extends BaseActivity {
    //回复列表
    private FrameLayout mReplyListContainer;
    //回复fragment
    private StoryReplyFragment storyReplyFragment;
    //回复内容编辑框
    private EditText mEtReplyContent;
    //回复button
    private TextView mTvReplyBtn;
    //storyid
    private Long storyid;

    private StoryService storyService = ServiceBuilder.builderStoryService(this);

    public void initData() {
        storyid = getIntent().getLongExtra("storyid", 0);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initWidget();
        initReplyList();
        initEditListener();
        initReplyBtnListener();
    }

    @Override
    public int getContentRid() {
        return R.layout.activity_story_reply;
    }

    /**
     * 初始化并替换Fragment
     */
    public void initReplyList() {
        storyReplyFragment = storyReplyFragment.create(storyid);
        getSupportFragmentManager().beginTransaction().replace(R.id.reply_list_container, storyReplyFragment).commit();

    }

    /**
     * 初始化组件信息
     */
    public void initWidget() {
        mReplyListContainer = (FrameLayout) findViewById(R.id.reply_list_container);
        mEtReplyContent = (EditText) findViewById(R.id.et_reply_content);
        mTvReplyBtn = (TextView) findViewById(R.id.tv_reply_btn);
    }

    @Override
    public void initListener() {

    }

    public void initEditListener() {
        mEtReplyContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取焦点
                InputMethodUtil.reuestFocus(mEtReplyContent);
            }
        });
    }

    /**
     * 组装评论对象
     *
     * @return
     */
    public StoryReply combReply() {
        StoryReply reply = new StoryReply();
        reply.desc = mEtReplyContent.getText().toString().trim();
        reply.storyid = storyid;
        return reply;
    }

    /**
     * 发送评论监听
     */
    public void initReplyBtnListener() {

        mTvReplyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //清除焦点并隐藏输入法
                InputMethodUtil.clearFocus(mEtReplyContent);
                final StoryReply reply = combReply();
                storyService.replyStory(reply, new AbResponseListener(new TypeReference<Result>() {
                }) {
                    @Override
                    public void responseInfo(Result data) {
                        storyReplyFragment.addReply(reply);
                    }
                });
            }
        });

    }

}
